package com.sifeng.utils;

import com.sifeng.enums.FileTypeEnum;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yang_gsh on 2018/9/11.
 */
public class MyUtils {
	public static List<String> readDirectory(String path, FileTypeEnum fileTypeEnum) {
		File file=new File(path);
		File[] tempList = file.listFiles();
		List<String> list = new ArrayList<String>();
		assert tempList != null;
		for (File value : tempList) {
			if (FileTypeEnum.FILE.equals(fileTypeEnum) && value.isFile()) {
				list.add(value.getName());
			}
			if (FileTypeEnum.DIRECTORY.equals(fileTypeEnum) && value.isDirectory()) {
				list.add(value.getName());
			}
		}

		return list;
	}

	public static List<String> searchFile(String path,String startStr,String middleStr, String endStr){
		File file=new File(path);
		File[] tempList = file.listFiles();
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < tempList.length; i++) {
			if (startStr != null && !tempList[i].getName().startsWith(startStr)) {
				continue;
			}

			if (middleStr != null && !tempList[i].getName().contains(middleStr)) {
				continue;
			}

			if (endStr != null && !tempList[i].getName().endsWith(endStr)) {
				continue;
			}

			list.add(tempList[i].getName());
		}

		return list;
	}

	public static List<String> searchFile(String path,String startStr) {
		return searchFile(path, startStr, null, null);
	}

	public static List<String> searchFile(String path,String startStr, String endStr) {
		return searchFile(path, startStr, null, endStr);
	}

	public static XSSFWorkbook getXSSFWorkbook(String path) throws IOException {
		InputStream iStream = new FileInputStream(path);
		return new XSSFWorkbook(iStream);
	}
}
